var table;
var obj;
layui.use('table', function(){
    table = layui.table
        ,form = layui.form;


    form.on('select(product)', function (data) {
        loadGuige(data.value);
    });

    form.on('select(guige)', function (data) {
        loadSum(data.value);
        Load(data.value);
    });

    loadProduct();

    function loadSum(guige) {
        $.ajax({
            url: "/activitySum?guige="+guige,
            dataType: "json",
            success: function (data) {
                if (data.code == "0") {
                    $("#sum").html("总销售量："+data.count);
                }
                else {
                    alert(data.msg);
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
            }
        });
    }

    function loadProduct() {
        $.ajax({
            url: "/activityProductList",
            dataType: "json",
            success: function (data) {
                if (data.code == "0") {
                    var option1 = $("<option>").val(0).text("选择活动产品");
                    $("#product").html(option1);
                    $.each(data.data, function (i, item) {
                        var option1 = $("<option>").val(item.productId).text(item.productName);
                        $("#product").append(option1);
                    });

                    form.render('select');
                }
                else {
                    alert(data.msg);
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
            }
        });
    }

    function loadGuige(productId) {
        $.ajax({
            url: "/activityGuigeList?productId="+productId,
            dataType: "json",
            success: function (data) {
                if (data.code == "0") {
                    var option1 = $("<option>").val(0).text("选择活动规格");
                    $("#guige").html(option1);
                    $.each(data.data, function (i, item) {
                        var option1 = $("<option>").val(item.guigeId).text(item.guigeName);
                        $("#guige").append(option1);
                    });

                    form.render('select');
                }
                else {
                    alert(data.msg);
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
            }
        });
    }

    function Load(guige) {
        table.render({
            elem: '#test'
            ,url:"/activityList?guige="+guige
            // ,cellMinWidth: 80
            ,title: '统计'
            ,totalRow: true
            ,cols: [[
                {type:'numbers'}
                // ,{type: 'checkbox'}
                ,{field:'id', title:'ID'}
                ,{field:'userNo', title:'用户编号'}
                ,{field:'name', title:'用户姓名'}
                ,{field:'sum', title:'套数'}
                ,{field:'sum', title:'销售量'}
            ]]
            ,page: true
            ,limit:15
            ,limits: [15,30,60,100]
            ,id: 'test1'
        });
    }

});
function s1(a) {
    return (a-(a/4));
}
function s2(a) {
return a/4;
}
function SelectData()
{
    LoadW();
    loadSumW();

}

function LoadW() {
    table.render({
        elem: '#test'
        ,url:"/wActivityList"
        // ,cellMinWidth: 80
        ,title: '统计'
        ,totalRow: true
        ,cols: [[
            {type:'numbers'}
            // ,{type: 'checkbox'}
            ,{field:'id', title:'ID'}
            ,{field:'userNo', title:'用户编号'}
            ,{field:'name', title:'用户姓名'}
            ,{field:'sum', title:'套数',templet :function (row){return s1(row.sum);}}
            ,{field:'sum', title:'销售量',templet :function (row){return s2(row.sum);}}
            ,{field:'sum', title:'提数',templet: '#barSum', unresize: true}
        ]]
        ,page: true
        ,limit:15
        ,limits: [15,30,60,100]
        ,id: 'test1'
    });
}

function loadSumW() {
    $.ajax({
        url: "/wActivitySum",
        dataType: "json",
        success: function (data) {
            if (data.code == "0") {
                $("#sum").html("总销售量："+data.count);
            }
            else {
                alert(data.msg);
            }
        },
        error: function (message) {
            alert("提交数据失败！"+message);
        }
    });
}


function WinOpenShow(uid) {
    layer.open({
        type: 2
        , title: '会员销售详情'
        , content: '/WActivityUserNoDetails?uid='+uid
        , area: ['700px', '500px']
        , btn: ['确定', '取消']
        , yes: function (index, layero) {
        }
    });
}

